using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class SendTask
    {
        public bool Exsits(int TaskID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int)
            };
            parameters[0].Value = TaskID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SendTask_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(SendTaskInfo SendTask)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@UserID",SqlDbType.VarChar,20),
                new SqlParameter("@DestNumber",SqlDbType.VarChar,500),
                new SqlParameter("@Content",SqlDbType.VarChar,500),
                new SqlParameter("@SignName",SqlDbType.VarChar,20),
                new SqlParameter("@SendPriority",SqlDbType.Int),
                new SqlParameter("@SendTime",SqlDbType.DateTime),
                new SqlParameter("@ValidMinute",SqlDbType.Int),
                new SqlParameter("@SendFlag",SqlDbType.Int),
                new SqlParameter("@CommPort",SqlDbType.Int),
                new SqlParameter("@SplitCount",SqlDbType.Int)
            };
            parameters[0].Value = SendTask.UserID;
            parameters[1].Value = SendTask.DestNumber;
            parameters[2].Value = SendTask.Content;
            parameters[3].Value = SendTask.SignName;
            parameters[4].Value = SendTask.SendPriority;
            parameters[5].Value = SendTask.SendTime;
            parameters[6].Value = SendTask.ValidMinute;
            parameters[7].Value = SendTask.SendFlag;
            parameters[8].Value = SendTask.CommPort;
            parameters[9].Value = SendTask.SplitCount;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SendTask_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(SendTaskInfo SendTask)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int),
                new SqlParameter("@UserID",SqlDbType.VarChar,20),
                new SqlParameter("@DestNumber",SqlDbType.VarChar,500),
                new SqlParameter("@Content",SqlDbType.VarChar,500),
                new SqlParameter("@SignName",SqlDbType.VarChar,20),
                new SqlParameter("@SendPriority",SqlDbType.Int),
                new SqlParameter("@SendTime",SqlDbType.DateTime),
                new SqlParameter("@ValidMinute",SqlDbType.Int),
                new SqlParameter("@SendFlag",SqlDbType.Int),
                new SqlParameter("@CommPort",SqlDbType.Int),
                new SqlParameter("@SplitCount",SqlDbType.Int)
            };
            parameters[0].Value = SendTask.TaskID;
            parameters[1].Value = SendTask.UserID;
            parameters[2].Value = SendTask.DestNumber;
            parameters[3].Value = SendTask.Content;
            parameters[4].Value = SendTask.SignName;
            parameters[5].Value = SendTask.SendPriority;
            parameters[6].Value = SendTask.SendTime;
            parameters[7].Value = SendTask.ValidMinute;
            parameters[8].Value = SendTask.SendFlag;
            parameters[9].Value = SendTask.CommPort;
            parameters[10].Value = SendTask.SplitCount;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SendTask_Update", parameters);
                return rowAffected > 0;
            }
        }

        public bool UpdateSendFlag(SendTaskInfo SendTask)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int),
                new SqlParameter("@SendFlag",SqlDbType.Int)
            };
            parameters[0].Value = SendTask.TaskID;
            parameters[1].Value = SendTask.SendFlag;

            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SendTask_UpdateSendFlag", parameters);
                return rowAffected > 0;
            }
        }

        public bool Delete(int TaskID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int)
            };
            parameters[0].Value = TaskID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "SendTask_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public SendTaskInfo GetSendTask(int TaskID)
        {
            SendTaskInfo SendTask = null;
            SqlParameter[] parameters = {
                new SqlParameter("@TaskID",SqlDbType.Int)
            };
            parameters[0].Value = TaskID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SendTask_GetDetail", parameters);
                if (dr.Read())
                {
                    SendTask = new SendTaskInfo();
                    SendTask.TaskID = dr.GetInt32(0);
                    SendTask.UserID = dr.GetString(1);
                    SendTask.DestNumber = dr.GetString(2);
                    SendTask.Content = dr.GetString(3);
                    SendTask.SignName = dr.GetString(4);
                    SendTask.SendPriority = dr.GetInt32(5);
                    SendTask.SendTime = dr.GetDateTime(6);
                    SendTask.ValidMinute = dr.GetInt32(7);
                    SendTask.SendFlag = dr.GetInt32(8);
                    SendTask.CommPort = dr.GetInt32(9);
                    SendTask.SplitCount = dr.GetInt32(10);
                }
                return SendTask;
            }
        }

        public List<SendTaskInfo> GetSendTask10(int falge1, int flage2)
        {
            List<SendTaskInfo> SendTasks = new List<SendTaskInfo>();
            SendTaskInfo SendTask = null;
            SqlParameter[] parameters = {
                                            new SqlParameter("@Flag", SqlDbType.Int),
                                            new SqlParameter("@Flag2", SqlDbType.Int)
                                        };
            parameters[0].Value = falge1;
            parameters[1].Value = flage2;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SendTask_Update10",
                                                           parameters);
                while (dr.Read())
                {
                    SendTask = new SendTaskInfo();
                    SendTask.TaskID = dr.GetInt32(0);
                    SendTask.UserID = dr.GetString(1);
                    SendTask.DestNumber = dr.GetString(2);
                    SendTask.Content = dr.GetString(3);
                    SendTask.SignName = dr.GetString(4);
                    SendTask.SendPriority = dr.GetInt32(5);
                    SendTask.SendTime = dr.GetDateTime(6);
                    SendTask.ValidMinute = dr.GetInt32(7);
                    SendTask.SendFlag = dr.GetInt32(8);
                    SendTask.CommPort = dr.GetInt32(9);
                    SendTask.SplitCount = dr.GetInt32(10);
                    SendTasks.Add(SendTask);
                }
                return SendTasks;
            }
        }


        public List<SendTaskInfo> GetList(string StrWhere)
        {
            List<SendTaskInfo> SendTasks = new List<SendTaskInfo>();
            SendTaskInfo SendTask = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "SendTask_GetList", parameters);
                while (dr.Read())
                {
                    SendTask = new SendTaskInfo();
                    SendTask.TaskID = dr.GetInt32(0);
                    SendTask.UserID = dr.GetString(1);
                    SendTask.DestNumber = dr.GetString(2);
                    SendTask.Content = dr.GetString(3);
                    SendTask.SignName = dr.GetString(4);
                    SendTask.SendPriority = dr.GetInt32(5);
                    SendTask.SendTime = dr.GetDateTime(6);
                    SendTask.ValidMinute = dr.GetInt32(7);
                    SendTask.SendFlag = dr.GetInt32(8);
                    SendTask.CommPort = dr.GetInt32(9);
                    SendTask.SplitCount = dr.GetInt32(10);
                    SendTasks.Add(SendTask);
                }
                return SendTasks;
            }
        }

    }
}